<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no, viewport-fit=cover" />
    <link rel="icon" type="image/png" href="/favicon.png" />
    <meta name="renderer" content="webkit" />
    <meta name="force-rendering" content="webkit" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <title>登录中</title>
    <link rel="stylesheet" href="/src/common/mdcss/freestyle.css" />
    <script src="/src/common/mdjs/freestyle.js"></script>
  </head>

  <body>
    <script>
      var __api_server__ = <%- apiServer %>;
      var getRequest = () => {
          var encodeUrl = new URL(location.href.replace('#', encodeURIComponent('#')));
          var search = encodeUrl.search.replace('?', '');
          var theRequest = new Object();
          var strs = search.split('&');
          for (var i = 0; i < strs.length; i++) {
              var result = strs[i].split('=');
              theRequest[result[0]] = decodeURIComponent(result[1]);
          }
          return theRequest;
      };
      var request = getRequest();

      function getGlobalMeta(cb) {
        var xhr = new XMLHttpRequest();
        xhr.open('POST', __api_server__.main + 'Global/GetGlobalMeta', true);
        xhr.setRequestHeader('Content-Type', 'application/json');

        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    var response = JSON.parse(xhr.responseText);
                    var data = response.data;
                    var ApiUrl = data['md.global'].Config.ApiUrl;
                    cb(ApiUrl);
                }
            }
        }
        xhr.send(JSON.stringify({}));
      }

      getGlobalMeta(function(ApiUrl) {
        var xhr = new XMLHttpRequest();
        xhr.open('POST', ApiUrl + '/oauth2/h5login', true);
        xhr.setRequestHeader('Content-Type', 'application/json');

        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    var response = JSON.parse(xhr.responseText);
                    if (response.success) {
                        var ua = navigator.userAgent;
                        var isIOS = !!ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
                        var info = {
                            sessionId: '',
                            type: 'native',
                            settings: {
                                action: 'login',
                                accessTokenInfo: response
                            }
                        };
                        var string = JSON.stringify(info);
                        var base64 = window.btoa(string);
                        if (isIOS) {
                            window.webkit.messageHandlers.MD_APP_REQUEST.postMessage(base64);
                        } else {
                            window.Android.MD_APP_REQUEST(base64);
                        }
                    } else {
                        alert('登录失败!');
                    }
                }
            }
        };

        xhr.send(JSON.stringify(request));
      });
    </script>
  </body>
</html>
